package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.ListUserRolesRequest;
import com.bestcem.bp.xm.user.grpc.v1.services.Role;
import com.bestcem.bp.xm.user.grpc.v1.services.RoleCondition;
import com.bestcem.bp.xm.user.grpc.v1.services.UserRole;
import com.bestcem.xm.user.enums.RoleBiShareEnum;
import com.bestcem.xm.user.grpc.service.converter.strategy.CommonStrategy;
import com.bestcem.xm.user.grpc.v1.enums.RoleIndexEnum;
import com.bestcem.xm.user.grpc.v1.enums.RoleStatusEnum;
import com.bestcem.xm.user.grpc.v1.enums.RoleTypeEnum;
import com.bestcem.xm.user.service.dto.role.UserRoleDTO;
import com.bestcem.xm.user.service.dto.user.UserUserRoleDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 角色参数转换
 *
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 14:04
 */
@Component
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserRoleConverter {

    public Role userRoleDto2Role(UserRoleDTO roleDTO) {
        Role.Builder builder = Role.newBuilder();
        if (StringUtils.isNotBlank(roleDTO.getRoleId())) {
            builder.setId(roleDTO.getRoleId());
        }
        if (StringUtils.isNotBlank(roleDTO.getOrgId())) {
            builder.setOrgId(roleDTO.getOrgId());
        }
        if (StringUtils.isNotBlank(roleDTO.getName())) {
            builder.setName(roleDTO.getName());
        }
        if (Objects.nonNull(roleDTO.getStatus())) {
            builder.setStatus(RoleStatusEnum.RoleStatus.forNumber(roleDTO.getStatus()));
        }
        if (Objects.nonNull(roleDTO.getType())) {
            builder.setType(RoleTypeEnum.RoleType.forNumber(roleDTO.getType()));
        }
        if (StringUtils.isNotBlank(roleDTO.getContent())) {
            builder.setContent(roleDTO.getContent());
        }
        if (Objects.nonNull(roleDTO.getRoleIndex())) {
            builder.setIndex(RoleIndexEnum.RoleIndex.forNumber(roleDTO.getRoleIndex()));
        }
        if (Objects.nonNull(roleDTO.getBiShare())) {
            builder.setBiShare(roleDTO.getBiShare().equals(RoleBiShareEnum.SHARE.getType()));
        }
        return builder.build();
    }

    public UserRoleDTO roleToUserRoleDTO(RoleCondition role) {
        UserRoleDTO roleDTO = new UserRoleDTO();
        roleDTO.setRoleId(role.getId());
        if (role.hasIndex()) {
            roleDTO.setRoleIndex(role.getIndexValue());
        }
        roleDTO.setOrgId(role.getOrgId());
        roleDTO.setName(role.getName());
        if (role.hasType()) {
            roleDTO.setType(role.getTypeValue());
        }
        if (role.hasStatus()) {
            roleDTO.setStatus(role.getStatusValue());
        }
        if (role.hasBiShare()) {
            roleDTO.setBiShare(role.getBiShare() ? RoleBiShareEnum.SHARE.getType() : RoleBiShareEnum.NO_SHARE.getType());
        }
        roleDTO.setContent(role.getContent());
        return roleDTO;
    }

    public List<Role> userRoleDtoToRoleList(List<UserRoleDTO> data) {
        List<Role> roleList = new ArrayList<>();
        for (UserRoleDTO roleDTO : data) {
            Role.Builder builder = Role.newBuilder();
            if (StringUtils.isNotBlank(roleDTO.getRoleId())) {
                builder.setId(roleDTO.getRoleId());
            }
            if (StringUtils.isNotBlank(roleDTO.getOrgId())) {
                builder.setOrgId(roleDTO.getOrgId());
            }
            if (StringUtils.isNotBlank(roleDTO.getName())) {
                builder.setName(roleDTO.getName());
            }
            if (Objects.nonNull(roleDTO.getStatus())) {
                builder.setStatus(RoleStatusEnum.RoleStatus.forNumber(roleDTO.getStatus()));
            }
            if (Objects.nonNull(roleDTO.getType())) {
                builder.setType(RoleTypeEnum.RoleType.forNumber(roleDTO.getType()));
            }
            if (StringUtils.isNotBlank(roleDTO.getContent())) {
                builder.setContent(roleDTO.getContent());
            }
            if (Objects.nonNull(roleDTO.getRoleIndex())) {
                builder.setIndex(RoleIndexEnum.RoleIndex.forNumber(roleDTO.getRoleIndex()));
            }
            if (Objects.nonNull(roleDTO.getBiShare())) {
                builder.setBiShare(roleDTO.getBiShare().equals(RoleBiShareEnum.SHARE.getType()));
            }
            roleList.add(builder.build());
        }
        return roleList;
    }

    public static UserUserRoleDTO listUserRoleReq2Dto(ListUserRolesRequest request) {
        if ( request == null ) {
            return null;
        }

        UserUserRoleDTO userUserRoleDTO = new UserUserRoleDTO();
        userUserRoleDTO.setUserRoleId( CommonStrategy.stringStrategy( request.getId() ) );
        userUserRoleDTO.setUserId( CommonStrategy.stringStrategy( request.getUserId() ) );
        userUserRoleDTO.setRoleId( CommonStrategy.stringStrategy( request.getRoleId() ) );

        return userUserRoleDTO;
    }

    public static UserRole userRoleDto2Grpc(UserUserRoleDTO userRoleDTO) {
        if ( userRoleDTO == null ) {
            return null;
        }

        com.bestcem.bp.xm.user.grpc.v1.services.UserRole.Builder userRole = UserRole.newBuilder();

        if ( userRoleDTO.getUserRoleId() != null ) {
            userRole.setId( userRoleDTO.getUserRoleId() );
        }
        if ( userRoleDTO.getUserId() != null ) {
            userRole.setUserId( userRoleDTO.getUserId() );
        }
        if ( userRoleDTO.getRoleId() != null ) {
            userRole.setRoleId( userRoleDTO.getRoleId() );
        }

        return userRole.build();
    }

    public static List<UserRole> userRoleDtos2Grpcs(List<UserUserRoleDTO> userRoleList) {
        if ( userRoleList == null ) {
            return Collections.emptyList();
        }

        List<UserRole> list = new ArrayList<>( userRoleList.size() );
        for ( UserUserRoleDTO userUserRoleDTO : userRoleList ) {
            list.add( userRoleDto2Grpc( userUserRoleDTO ) );
        }

        return list;
    }
}
